{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8882fe91",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679282145,dc08c022-0da3-43ba-9ab5-f4a9c8d73bbb', 'time_used': 5, 'error_message': 'MISSING_ARGUMENTS: face_token1, image_base64_1, image_url1, image_file1'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸对比\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('gao_01.jpg', 'rb')\n",
    "f_02 = open('gao_02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "params = {\n",
    "    \"api_key\":\"zm08EBCxK4rS7R35ks2u6OzAUh6mcfwy\",\n",
    "    \"api_secret\":\"esQp7sClEenWKMiw4Rt1x1PXX_ymgnFC\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e17cb10",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "334a98b6",
   "metadata": {},
   "source": [
    "## 2.1人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d97ae3c",
   "metadata": {},
   "source": [
    "## 2.1.1创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8474001c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': 'dba850e13616e80788754082a37e38ed', 'time_used': 241, 'face_count': 0, 'face_added': 0, 'request_id': '1679284228,400939b5-d5d8-456d-862f-375bf3fcfc98', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "'''\n",
    "人脸数据创建\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"zm08EBCxK4rS7R35ks2u6OzAUh6mcfwy\",\n",
    "    \"api_secret\":\"esQp7sClEenWKMiw4Rt1x1PXX_ymgnFC\",\n",
    "    \"display_name\":\"广州南方学院1栋宿舍楼\"\n",
    "}\n",
    "response = requests.post(request_url, data = params)\n",
    "\n",
    "print (response.status_code)\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23c567e5",
   "metadata": {},
   "source": [
    "## 2.1.2人脸数据集合详情查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d4bf7b60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1679284251,4c51275d-3d32-45be-b873-7b90f99fa329',\n",
       " 'time_used': 84,\n",
       " 'error_message': 'MISSING_ARGUMENTS: image_url, image_file, image_base64'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"zm08EBCxK4rS7R35ks2u6OzAUh6mcfwy\",\n",
    "    \"api_secret\":\"esQp7sClEenWKMiw4Rt1x1PXX_ymgnFC\",\n",
    "    \"faceset_token\":\"1e6cd502bcad0d6cc54e42633b3b9855\"\n",
    "}\n",
    "response = requests.post(request_url, data = params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bd22f45",
   "metadata": {},
   "source": [
    "### 2.1.3增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e99d0d5b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'img' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [26]\u001b[0m, in \u001b[0;36m<cell line: 14>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     10\u001b[0m img_01 \u001b[38;5;241m=\u001b[39m base64\u001b[38;5;241m.\u001b[39mb64encode(f_01\u001b[38;5;241m.\u001b[39mread())\n\u001b[0;32m     11\u001b[0m img_02 \u001b[38;5;241m=\u001b[39m base64\u001b[38;5;241m.\u001b[39mb64encode(f_02\u001b[38;5;241m.\u001b[39mread())\n\u001b[0;32m     13\u001b[0m params \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m     14\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapi_key\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzm08EBCxK4rS7R35ks2u6OzAUh6mcfwy\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     15\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapi_secret\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mesQp7sClEenWKMiw4Rt1x1PXX_ymgnFC\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m---> 16\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimage_base64\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[43mimg\u001b[49m,\n\u001b[0;32m     17\u001b[0m }\n\u001b[0;32m     18\u001b[0m response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mpost(request_url, data \u001b[38;5;241m=\u001b[39m params)\n\u001b[0;32m     20\u001b[0m \u001b[38;5;28mprint\u001b[39m (response\u001b[38;5;241m.\u001b[39mstatus_code)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'img' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "import requests\n",
    "import base64\n",
    "'''\n",
    "人脸检测：刘亦菲、蔡徐坤、高启强\n",
    "'''\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "f_01 = open('gao_01.jpg', 'rb')\n",
    "f = open('gao_02.jpg', 'rb')\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"zm08EBCxK4rS7R35ks2u6OzAUh6mcfwy\",\n",
    "    \"api_secret\":\"esQp7sClEenWKMiw4Rt1x1PXX_ymgnFC\",\n",
    "    \"image_base64\":img,\n",
    "}\n",
    "response = requests.post(request_url, data = params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c749b18",
   "metadata": {},
   "source": [
    "### 2.1.4人脸搜索（课后作业）"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "原始单元格格式",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
